package SewerAgent;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;

import java.util.Calendar;
import java.util.Random;

import wwtp.ontology.Biological;
import wwtp.ontology.Chemical;
import wwtp.ontology.Hour;
import wwtp.ontology.Origin;
import wwtp.ontology.Physical;
import wwtp.ontology.SendWater;
import wwtp.ontology.Volume;
import wwtp.ontology.Water;


public class DrainageBehaviour extends TickerBehaviour{

	public DrainageBehaviour(Agent arg0, long arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onTick() {
		// TODO Auto-generated method stub
		/*
		 * water generation
		 */	
		System.out.println("\n"+myAgent.getLocalName() + " Drainage -> " + ((SewerAgent)myAgent).dbDirectWater.getDischargeTank(myAgent.getLocalName()) );
		
		Random rand = new Random();
		
		SendWater  sw = new SendWater();
		Water 		w = new Water();
		Volume 		v = new Volume();
		Chemical 	c = new Chemical();
		Biological 	b = new Biological();
		Physical 	p = new Physical();
		
		
		Hour 	    h = new Hour();
		Origin	    o = new Origin();	
		
		Calendar date = Calendar.getInstance();
		
		o.setName(myAgent.getLocalName());		
		h.setHours(date.get(Calendar.HOUR_OF_DAY));
		h.setMinutes(date.get(Calendar.MINUTE));
		h.setSeconds(date.get(Calendar.SECOND));

		float litres = 1;
		
		if(((SewerAgent)myAgent).mildthunderstorm == false){
			litres = 20 + rand.nextInt(45)+1;
			if(litres > 60){
				((SewerAgent)myAgent).mildthunderstorm = true;
			}
		}
		else{
			System.out.println(myAgent.getLocalName() + " MILDTHUNDERSTORM");
			litres = 60 + rand.nextInt(10)+1;
			if (rand.nextInt(2) == 1){
				((SewerAgent)myAgent).mildthunderstorm = false;
			}
		}
		
		v.setLitres(litres); // litros > 0
		
		

		c.setPercentaje(rand.nextInt(11));
		b.setPercentaje(rand.nextInt(11));
		p.setPercentaje(rand.nextInt(11));	

		w.setVolume(v);
		w.setChemical(c);
		w.setBiological(b);
		w.setPhysical(p);
		
		sw.setWater(w);
		sw.setOrigin(o);
		sw.setHour(h);
		
		ACLMessage waterMsg = new ACLMessage( ACLMessage.INFORM );
		waterMsg.setConversationId("Water");
		waterMsg.setLanguage(((SewerAgent)myAgent).codec.getName());
		waterMsg.setOntology(((SewerAgent)myAgent).ontology.getName());
		
		waterMsg.addReceiver(new AID( ((SewerAgent)myAgent).dbDirectWater.getDischargeTank(myAgent.getLocalName()),AID.ISLOCALNAME) );
		
		try{
			
			myAgent.getContentManager().fillContent(waterMsg,sw);
		
			myAgent.send(waterMsg);
					
			int notification = rand.nextInt(3);
			if (notification != 0){
				ACLMessage notificationMsg = new ACLMessage( ACLMessage.INFORM );
				notificationMsg.setConversationId("Notification");
				notificationMsg.setLanguage(((SewerAgent)myAgent).codec.getName());
				notificationMsg.setOntology(((SewerAgent)myAgent).ontology.getName());	
				notificationMsg.addReceiver(new AID("Authority",AID.ISLOCALNAME) );	
				
				System.out.println(myAgent.getLocalName() + " sending NOTIFICATION of toxic effluent to Authority");
				myAgent.getContentManager().fillContent(notificationMsg,sw);
				myAgent.send(notificationMsg);
			}
			else{
				System.out.println(myAgent.getLocalName() + " NON-NOTIFIED TOXIC EFFUENT discharged");
			}
			
			System.out.println("");
		}
		catch (Exception e){
			e.printStackTrace();
		}	
	}


}
